﻿/********************************************************************************
 * CoTime. All rights reserved.
 * Author: 刘冰
 * Email : ice6@live.cn
 * Description:
 *     针对View xaml结构的一些扩展方法
 *     作用是为了让程序看起来更有表达力
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace CoTime
{
    public static class UITreeHelper
    {
        public static T FindChildByName<T>(FrameworkElement parent, string name) where T : FrameworkElement
        {
            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;
                if (child != null &&
                    child is T &&
                    string.Equals(name, child.Name))
                    return (T)child;
                else
                {
                    var childOfChild = FindChildByName<T>(child, name);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
        public static T Find<T>(this FrameworkElement parent, string name) where T : FrameworkElement
        {
            return UITreeHelper.FindChildByName<T>(parent, name);
        }

        public static List<T> FindAll<T>(this FrameworkElement parent) where T : FrameworkElement
        { 
            return FindAll<T>(parent,false);
        }

        public static List<T> FindAll<T>(this FrameworkElement parent, Boolean includeChildren) where T : FrameworkElement
        {
            var list = new List<T>();
            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;
                if (child != null && child is T)
                    list.Add((T)child);
                else if (includeChildren)
                {
                    T childOfChild = First<T>(child);
                    if (childOfChild != null)
                       list.Add((T)childOfChild);
                }
            }
            return list;
        }

        public static T FindAnchestor<T>(this DependencyObject current) where T : DependencyObject
        {
            do
            {
                if (current is T)
                {
                    return (T)current;
                }
                current = VisualTreeHelper.GetParent(current);
            }
            while (current != null);
            return null;
        }


        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent">default false</param>
        /// <returns></returns>
        public static T First<T>(this FrameworkElement parent) where T : FrameworkElement
        {
            return First<T>(parent, false);
        }
        public static T First<T>(this FrameworkElement parent, Boolean includeChildren) where T : FrameworkElement
        {
            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;
                if (child != null && child is T)
                    return (T)child;
                else if (includeChildren)
                {
                    var childOfChild = First<T>(child);
                    if (childOfChild != null)
                        return (T)childOfChild;
                }
            }
            return null;
        }
    }
}
